using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.AI.Node
{
    [Category("Ggame/Liberty")]
    public class SetAnimatorTrigger : AIBaseNode
    {
        [RequiredField]
        public BBParameter<string> triggerName;
        public BBParameter<bool> triggerValue = true;
        private AnimatorControllerParameter[] animatorControllerParameters;

        protected override string OnInit()
        {
            string s = base.OnInit();
            // _animator.parameters GC.Alloc 1.1kb
            animatorControllerParameters = _animator.parameters;
            return s;
        }

        protected override void OnExecute()
        {
            foreach (var param in animatorControllerParameters)
            {
                if (param.type == AnimatorControllerParameterType.Trigger)
                {
                    _animator.ResetTrigger(param.name);
                }
            }
            //
            // // fix: Animator组件Trigger有时不会重置，加上容错
            // _animator.ResetTrigger(triggerName.value);
            if (triggerValue.value) {
                _animator.SetTrigger(triggerName.value);
            }
            EndAction(true);
        }
        
    }
}